package com.sczmgk.syevaluation.activity.base;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sczmgk.syevaluation.R;
import com.sczmgk.syevaluation.activity.Interf.ActivityCallback;
import com.sczmgk.syevaluation.activity.LoginActivity;
import com.sczmgk.syevaluation.activity.LogoutActivity;
import com.sczmgk.syevaluation.activity.StaffActivity;
import com.sczmgk.syevaluation.dialog.DialogFactory;
import com.sczmgk.syevaluation.dialog.MessageDialog;
import com.sczmgk.syevaluation.entity.base.BaseEntity;
import com.sczmgk.syevaluation.entity.input.LoginInput;
import com.sczmgk.syevaluation.entity.output.AppVersion;
import com.sczmgk.syevaluation.entity.output.LoginStatus;
import com.sczmgk.syevaluation.entity.output.UserMetter;
import com.sczmgk.syevaluation.entity.output.UserMetterBean;
import com.sczmgk.syevaluation.utils.CommonUtils;
import com.sczmgk.syevaluation.utils.HttpTools;
import com.sczmgk.syevaluation.utils.MyLog;
import com.sczmgk.syevaluation.utils.SoundPlayUtils;
import com.sczmgk.syevaluation.utils.SpfUtil;
import com.sczmgk.syevaluation.utils.Tools;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import jp.wasabeef.glide.transformations.CropTransformation;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;
import static com.sczmgk.syevaluation.utils.FileUtil.getAPKPath;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, ActivityCallback {

    private boolean mouseLeftClick = false;
    private boolean soundPlayed = false;
    /**
     * 正在加载的对话框
     */
    private Dialog loadingDialog;
    /**
     * 该窗口能办理的业务
     */
    private TextView metters;
    /**
     * 用户名
     */
    private TextView name;
    /**
     * 工号
     */
    private TextView jobnum;
    /**
     * 用户头像
     */
    private ImageView head;
    /**
     * 评价按钮
     */
    private ImageView pj1, pj2, pj3, pj4, pj5;
    /**
     * 窗口编号
     */
    private TextView No;
    /**
     * 窗口名称
     */
    private TextView windowName;
    /**
     * 部门
     */
    private TextView department;

    /**
     * 获取登录状态定时器
     */
    private Timer timer_login_status;
    /**
     * 评价成功后再次评价倒计时
     */
    private Timer timer_countdown;
    /**
     * 定时拉取是否有评价请求
     */
    private Timer timer_evaluate;
    /**
     * 挂载poP容器
     */
    private View Container;
    /**
     * 倒计时秒的控件
     */
    private TextView Sec;
    private PopupWindow mPop;

    private UserMetterBean userMetterBean;
    private int time = 30;
    /**
     * //TODO  单屏设置成true   双屏设置成 false
     * 是否可以评价（双屏）单屏设置为： true  双屏设置成  false
     */
    private boolean evaluateEnable = false;
    private static final String TAG = BaseActivity.class.getSimpleName();
    private boolean isFirstBack = true;
    /**
     * 用户评价的容器
     */
    private LinearLayout mCustomerContainer;
    /**
     * 工作端发起评价的容器
     */
    private LinearLayout mStaffContainer;
    /**
     * 工作端发起评价的按钮
     */
    private Button evaluateBtn;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Sec.setText(time + "");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        userMetterBean = SpfUtil.getLoginInfo(BaseActivity.this);


        if (userMetterBean != null) {
            MyLog.i(TAG, "name：" + userMetterBean.name + " headpath: " + userMetterBean.headpath + "    metters:   " + userMetterBean.metters + "   department:    " + userMetterBean.department + "      worknum:    " + userMetterBean.worknum + "   windownum: " + userMetterBean.windownum);
        }

        initPopUp();
        initView();
        CommonUtils.addActivity(this);
        setListener();

        timer_login_status = new Timer();
        //开启定时器查询登录状态
        timer_login_status.schedule(new TimerTask() {
            @Override
            public void run() {
                queryLoginStatus();
            }


        }, 10 * 1000, 5000);
        timer_evaluate = new Timer();

        if (getActivityType() == SpfUtil.USER_CUSTOMER) {//客户端才需要定时查询评价请求
            //开启定时器查询评价请求
            timer_evaluate.schedule(new TimerTask() {
                @Override
                public void run() {
                    queryEvaluate();
                    //TODO
                    //1. 后台应做定时任务（30秒后无操作将状态置不可评价）
                    //2. 收到用户评价后设置为不可评价
                }


            }, 5 * 1000, 1000);
        }
        loadingDialog = DialogFactory.getLoadingDialog(this, R.string.loading_tip);
        SoundPlayUtils.init(this);

        mHandler.postDelayed(new Runnable() {//拉取用户及窗口信息
            @Override
            public void run() {
                checkVersion();
            }
        }, 5 * 1000);
        getDeaprtmentInfo();
    }

    /**
     * 获取用户数据，及部门窗口数据
     */
    private void getDeaprtmentInfo() {
        LoginInput loginInput = new LoginInput();
        loginInput.worknum = userMetterBean.worknum;
        loginInput.windownum = userMetterBean.windownum;
        loginInput.device = Tools.getMacAddress();
        Call<UserMetter> call = HttpTools.getService().updataWindowInfo(loginInput);
        call.enqueue(new Callback<UserMetter>() {
            @Override
            public void onResponse(Call<UserMetter> call, Response<UserMetter> response) {

                if (response.code() == 200) {
                    if (response.body().code == 1) {

                        MyLog.d(TAG, "重新获取用户及窗口数据 ================");
                        SpfUtil.saveLoginInfo(BaseActivity.this, response.body());
                        userMetterBean = SpfUtil.getLoginInfo(BaseActivity.this);
                        refreshUI();

                    } else {
                        //nothing
                    }
                } else {
                    //nothing
                }

            }

            @Override
            public void onFailure(Call<UserMetter> call, Throwable t) {
                // nothing
            }
        });
    }


    private void initPopUp() {
        mPop = new PopupWindow();
        View view = View.inflate(BaseActivity.this, R.layout.pop, null);
        Sec = view.findViewById(R.id.tv_sec);
        mPop.setContentView(view);
        mPop.setHeight(752);
        mPop.setWidth(1280);
        mPop.setOutsideTouchable(false);
    }

    /**
     * 查询登录状态
     */
    private void queryLoginStatus() {
        LoginInput input = new LoginInput();
        input.device = Tools.getMacAddress();
        input.windownum = userMetterBean.windownum;
        input.worknum = userMetterBean.worknum;
        input.type = (SpfUtil.getUserType(BaseActivity.this) == SpfUtil.USER_CUSTOMER ? 0 : 1);
        Call<LoginStatus> call = HttpTools.getService().getLoginStatus(input);
        call.enqueue(new Callback<LoginStatus>() {
            @Override
            public void onResponse(Call<LoginStatus> call, Response<LoginStatus> response) {
                if (response.code() == 200) {
                    if (response.body() != null && response.body().data != null) {
                        if (response.body().data.status == 0) {
                            if (timer_login_status != null) {
                                timer_login_status.cancel();
                            }
                            SpfUtil.setLoginStatus(BaseActivity.this, false);
                            showLogoutDialog();
                        }
                    }
                } else {
                    //   DialogFactory.warningDialog(MainActivity.this, response.message());
                }


            }

            @Override
            public void onFailure(Call<LoginStatus> call, Throwable t) {

            }
        });
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            evaluateEnable = false;
        }
    };
    /**
     * 查询是否能评价
     */
    private void queryEvaluate() {

        if (!evaluateEnable) {
            LoginInput input = new LoginInput();
            input.device = Tools.getMacAddress();
            input.windownum = userMetterBean.windownum;
            input.worknum = userMetterBean.worknum;
            Call<LoginStatus> call = HttpTools.getService().getCommand(input);
            call.enqueue(new Callback<LoginStatus>() {
                @Override
                public void onResponse(Call<LoginStatus> call, Response<LoginStatus> response) {
                    if (response.code() == 200) {
                        if (response.body() != null && response.body().data != null) {
                            if (response.body().data.status == 1) {
                                //设置可评价
                                //开启计时器，如30秒内设置为false 长时间用户不评价
                                 play();
                                mHandler.removeCallbacks(runnable);
                                mHandler.postDelayed(runnable, 20 * 1000);
                            }
                        }
                    } else {
                        //   DialogFactory.warningDialog(MainActivity.this, response.message());
                        MyLog.e(TAG,"请求失败  error code: "+response.code());
                    }


                }

                @Override
                public void onFailure(Call<LoginStatus> call, Throwable t) {
                    MyLog.e(TAG,"请求失败："+t.getMessage());
                }
            });
        }

    }

    private void setListener() {
        pj1.setOnClickListener(this);
        pj2.setOnClickListener(this);
        pj3.setOnClickListener(this);
        pj4.setOnClickListener(this);
        pj5.setOnClickListener(this);
        head.setOnClickListener(this);
        department.setOnClickListener(this);
        jobnum.setOnClickListener(this);
        evaluateBtn.setOnClickListener(this);
//        Container.setOnHoverListener(new View.OnHoverListener() {
//            @Override
//            public boolean onHover(View view, MotionEvent motionEvent) {
//                switch (motionEvent.getAction()){
//                    case MotionEvent.ACTION_HOVER_ENTER:
//                        MyLog.d(TAG,"进入");
//                        break;
//                    case MotionEvent.ACTION_HOVER_EXIT:
//                        MyLog.e(TAG,"退出");
//                        break;
//                    case MotionEvent.ACTION_HOVER_MOVE:
//                        MyLog.i(TAG,"移动");
//                        break;
//                        default:
//                            MyLog.i(TAG,"鼠标其它事件");
//                            break;
//                }
//                return false;
//            }
//        });

        Container.setOnGenericMotionListener(new View.OnGenericMotionListener() {

            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {
                int what = event.getButtonState();
                switch (what) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.BUTTON_PRIMARY:
                        MyLog.i(TAG, "鼠标左键单击事件");

                        play();
                        break;
                    case MotionEvent.BUTTON_TERTIARY:
                        MyLog.i(TAG, "鼠标中键单击事件");
                        break;
                    case MotionEvent.BUTTON_SECONDARY:
                        MyLog.i(TAG, "鼠标右键单击事件");
                        break;

                }
                return false;
            }
        });
    }

    private Runnable mouseClickRunnable = new Runnable() {
        @Override
        public void run() {
            mouseLeftClick = false;
        }
    };
    private Runnable playSoundRunnable = new Runnable() {
        @Override
        public void run() {
            soundPlayed = false;
        }
    };

    private void play() {
        mouseLeftClick = true;
        mHandler.postDelayed(playSoundRunnable, 5 * 1000);
        mHandler.removeCallbacks(mouseClickRunnable);
        mHandler.postDelayed(mouseClickRunnable, 1000);
        if (!soundPlayed) {
            soundPlayed = true;
            evaluateEnable = true;
            SoundPlayUtils.play(1);
        } else {
            MyLog.e(TAG, "正在播放--无法再次播放");
        }
    }

    private void initView() {
        evaluateBtn = findViewById(R.id.bt_evaluate);
        metters = findViewById(R.id.tv_metters);
        name = findViewById(R.id.tv_name);
        jobnum = findViewById(R.id.tv_jobnum);
        head = findViewById(R.id.iv_head);
        pj1 = findViewById(R.id.iv_pj1);
        pj2 = findViewById(R.id.iv_pj2);
        pj3 = findViewById(R.id.iv_pj3);
        pj4 = findViewById(R.id.iv_pj4);
        pj5 = findViewById(R.id.iv_pj5);
        windowName = findViewById(R.id.tv_window_name);
        department = findViewById(R.id.tv_department);
        Container = findViewById(R.id.container);
        No = findViewById(R.id.tv_no);
        mCustomerContainer = findViewById(R.id.ll_customer_container);
        mStaffContainer = findViewById(R.id.ll_staff_container);
        if (getActivityType() == SpfUtil.USER_CUSTOMER) {
            mCustomerContainer.setVisibility(View.VISIBLE);
            mStaffContainer.setVisibility(View.GONE);
        } else {
            mCustomerContainer.setVisibility(View.GONE);
            mStaffContainer.setVisibility(View.VISIBLE);
        }

        refreshUI();

    }

    private void refreshUI() {
        MyLog.d(TAG, "刷新界面--------");
        if (userMetterBean != null) {
            time = userMetterBean.pushtime;
            Sec.setText(time + "");
            metters.setText(userMetterBean.metters);
            name.setText("姓名：" + userMetterBean.name);
            jobnum.setText("工号：" + userMetterBean.worknum);
            windowName.setText(userMetterBean.windowname);
            department.setText(userMetterBean.department);
            No.setText(userMetterBean.windownum);

            Glide.with(this).load(userMetterBean.headpath).into(head);
            //圆图片
            // Glide.with(this).load(userMetterBean.headpath).apply(bitmapTransform(new CropTransformation(200, 200)).circleCrop()).into(head);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            //处理返回键
//            new Timer().schedule(new TimerTask() {
//                @Override
//                public void run() {
//                    isFirstBack = true;
//                }
//            }, 2000);
//            if (isFirstBack) {
//                MyLog.d(TAG, " 第一次按 Back   isFirstBack:" + isFirstBack);
//                MyToast.show(MainActivity.this, getString(R.string.main_goback_hint));
//                isFirstBack = false;
//                return true;
//            } else {
//                MyLog.d(TAG, " -----------第二次按 Back   isFirstBack:" + isFirstBack);
//                return super.onKeyDown(keyCode, event);
//            }

            MessageDialog dia = DialogFactory.chooseDialog(BaseActivity.this, "确定要退出吗?", "是", "否", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            dia.show();
            return true;

        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_pj1:
                commit(5);
                break;
            case R.id.iv_pj2:
                commit(4);
                break;
            case R.id.iv_pj3:
                commit(3);
                break;
            case R.id.iv_pj4:
                commit(2);
                break;
            case R.id.iv_pj5:
                commit(1);
                break;
            case R.id.iv_head:
                // SoundPlayUtils.play(1);
                break;
            case R.id.tv_jobnum:
                //进入登录登录界面
                Intent intent = new Intent(BaseActivity.this, LogoutActivity.class);

                startActivity(intent);
                break;
            case R.id.bt_evaluate:
                //TODO 增加提交评价请求的接口
                DialogFactory.showDialog(BaseActivity.this, loadingDialog);
                LoginInput input = new LoginInput();
                input.worknum = userMetterBean.worknum;
                Call<BaseEntity<Object>> baseEntityCall = HttpTools.getService().sendCommand(input);
                baseEntityCall.enqueue(new Callback<BaseEntity<Object>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                        DialogFactory.dismissLoadingDialog(BaseActivity.this, loadingDialog);

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {

                        DialogFactory.dismissLoadingDialog(BaseActivity.this, loadingDialog);
                        DialogFactory.warningDialog(BaseActivity.this, "发送指令失败：" + t.getMessage());
                    }
                });

//                DialogFactory.warningDialog(BaseActivity.this, "提示", "暂未开通工作端请点击确定返回至登录界面,并选择《用户端》重新登录", "确定", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        SpfUtil.setUserType(BaseActivity.this,SpfUtil.USER_CUSTOMER);
//                        SpfUtil.setLoginStatus(BaseActivity.this, false);
//                        Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
//                        startActivity(intent);
//                        finish();
//                    }
//                });
                break;
        }
    }

    /**
     * 提交评价
     *
     * @param level
     */
    private void commit(int level) {
        if (mouseLeftClick) {
            return;
        }

        if (!evaluateEnable) {
            return;
        }
        if (mPop != null && mPop.isShowing()) {
            return;
        }
        loadingDialog.show();
        LoginInput input = new LoginInput();
        input.device = Tools.getMacAddress();
        input.windownum = userMetterBean.windownum;
        input.worknum = userMetterBean.worknum;
        input.level = level;

        Call<BaseEntity<Object>> call = HttpTools.getService().commit(input);
        call.enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogFactory.dismissLoadingDialog(BaseActivity.this, loadingDialog);
                if (response.code() == 200) {
                    if (response.body().code == 1) {
                        // DialogFactory.warningDialog(MainActivity.this,"提交成功");
//                        Intent intent = new Intent(MainActivity.this, WaitingActivity.class);
//                        startActivity(intent);

                        if (!mPop.isShowing()) {
                            mPop.showAtLocation(Container, Gravity.CENTER, 0, 0);
                            //开启定时器
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (mPop.isShowing()) {
                                        mPop.dismiss();
                                        timer_countdown.cancel();
                                        timer_countdown = null;
                                        time = userMetterBean.pushtime;
                                    }
                                }
                            }, userMetterBean.pushtime * 1000);

                            timer_countdown = new Timer();
                            timer_countdown.schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    time--;
                                    // Sec.setText(time+"");
                                    Message msg = Message.obtain();
                                    mHandler.sendEmptyMessage(1);
                                }
                            }, 1000, 1000);
                        }

                        SoundPlayUtils.play(2);
                        evaluateEnable = false;  //TODO 评价完成，设置成不可评价
                    } else {
                        DialogFactory.warningDialog(BaseActivity.this, "失败： " + response.body().msg);
                    }
                } else {
                    DialogFactory.warningDialog(BaseActivity.this, response.message());
                }


            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogFactory.dismissLoadingDialog(BaseActivity.this, loadingDialog);
                DialogFactory.warningDialog(BaseActivity.this, "失败： " + t.getMessage());
            }
        });

    }

    private void showLogoutDialog() {
        DialogFactory.warningDialog(BaseActivity.this, "提示", "你的帐号在其它设备上登录,点击确定退出！", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
                startActivity(intent);
                CommonUtils.finishActivity();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer_countdown != null) {
            timer_countdown.cancel();
            timer_countdown = null;
        }
        if (timer_login_status != null) {
            timer_login_status.cancel();
            timer_login_status = null;
        }
        if (timer_evaluate != null) {
            timer_evaluate.cancel();
            timer_evaluate = null;
        }
    }

    /**
     * 检查版本更新
     */
    private void checkVersion() {
        Call<AppVersion> call = HttpTools.getService().getVersion();
        call.enqueue(new Callback<AppVersion>() {
            @Override
            public void onResponse(Call<AppVersion> call, final Response<AppVersion> response) {

                if (response.code() == 200) {
                    if (response.body().code == 1) {
                        MyLog.d(TAG, "versionCode: " + response.body().data.version + "    url: " + response.body().data.url);

                        //对比版本号
                        int appVersionCode = Tools.getAppVersionCode(BaseActivity.this);
                        if (response.body().data.version > appVersionCode) {
                            MyLog.d(TAG, "有新版本，正在下载更新--");
                            ///无进度条
                            Call<ResponseBody> responseBodyCall = HttpTools.getService().downloadFile(response.body().data.url);
                            responseBodyCall.enqueue(new Callback<ResponseBody>() {
                                @Override
                                public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                InputStream is = response.body().byteStream();
                                                File file = new File(getAPKPath());
                                                MyLog.i(TAG, "安装包： " + file.getPath());
                                                FileOutputStream fos = new FileOutputStream(file);
                                                BufferedInputStream bis = new BufferedInputStream(is);
                                                byte[] buffer = new byte[1024];
                                                int len;
                                                while ((len = bis.read(buffer)) != -1) {
                                                    fos.write(buffer, 0, len);
                                                    fos.flush();
                                                }
                                                fos.close();
                                                bis.close();
                                                is.close();
                                                installApk();
                                                //   installApp(getAPKPath());
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            MyLog.e(TAG, "success----------------");
                                        }
                                    }).start();
                                }

                                @Override
                                public void onFailure(Call<ResponseBody> call, Throwable t) {

                                }
                            });

                            /////有进度条
                            //下载
//                                    Call<ResponseBody> responseBodyCall = DownLoadApi.getService().downloadFile(response.body().data.url);
//
//                                    responseBodyCall.enqueue(new Callback<ResponseBody>() {
//                                        @Override
//                                        public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
//                                             new Thread(new Runnable() {
//                                                 @Override
//                                                 public void run() {
//                                                     try {
//                                                         InputStream is = response.body().byteStream();
//                                                         File file = new File(getAPKPath(), "评价器.apk");
//                                                         MyLog.i(TAG,"安装包： "+file.getPath());
//                                                         FileOutputStream fos = new FileOutputStream(file);
//                                                         BufferedInputStream bis = new BufferedInputStream(is);
//                                                         byte[] buffer = new byte[1024];
//                                                         int len;
//                                                         while ((len = bis.read(buffer)) != -1) {
//                                                             fos.write(buffer, 0, len);
//                                                             fos.flush();
//                                                         }
//                                                         fos.close();
//                                                         bis.close();
//                                                         is.close();
//                                                     } catch (IOException e) {
//                                                         e.printStackTrace();
//                                                     }
//                                                 }
//                                             }).start();
//                                            Log.e(TAG,"success");
//                                        }
//
//                                        @Override
//                                        public void onFailure(Call<ResponseBody> call, Throwable t) {
//
//                                        }
//                                    });
                        } else {
                            MyLog.e(TAG, "已是最新版本，无需更新---");
                        }

                    } else {
                        MyLog.e(TAG, "请求失败------");
                    }
                }

            }

            @Override
            public void onFailure(Call<AppVersion> call, Throwable t) {

            }
        });
    }


    /**
     * 安装apk
     */
    private void installApk() {


        if (Build.VERSION.SDK_INT >= 24) {//判读版本是否在7.0以上
            File file = new File(getAPKPath());
            Uri apkUri = FileProvider.getUriForFile(this, "com.sczmgk.syevaluation.fileprovider", file);//在AndroidManifest中的android:authorities值
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            startActivity(install);
        } else {
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setDataAndType(Uri.fromFile(new File(getAPKPath())), "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(install);
        }
    }
}
